﻿package com.vigmonk.utils
{
	import flash.display.DisplayObjectContainer;	
	
	import com.vigmonk.events.CustomEvent;
	
	import flash.display.StageAlign;
	import flash.display.StageDisplayState;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.TimerEvent;
	import flash.utils.Timer;	

	/**
	 * 
	 * StageManager.as
	 * @author VIGMONK
	 * 
	 */
	 
	 /**
	  * 
	  * import com.vigmonk.utils.StageManager;
	  * import com.vigmonk.events.CustomEvent;
	  * 
	  * var stageManager:StageManager = new StageManager();
	  * 
	  * // 일반
	  * stageManager.init(this);
	  * 
	  * // 리사이즈 체크 필요 시.
	  * stageManager.init(this,true);
	  * stageManager.addEventListener(StageManager.STAGE_RESIZE,stgResize);
	  * 
	  * 
	  * function stgResize(_e:CustomEvent):void
	  * {
	  * 	trace('[STAGE RESIZE] = ',_e.data.size);
	  * }
	  * 
	  * // 전체화면 실행
	  * btn.addEventListener(MouseEvent.MOUSE_DOWN,btnDown);
	  * function mouseDown(_e:MouseEvent):void
	  * {
	  * 	stageManager.goFullScreen();
	  * }
	  * 
	  */
	  
	public class StageManager extends EventDispatcher
	{
		public static const STAGE_RESIZE:String = 'STAGE_RESIZE';
		
		private var timer:Timer;
		private var scope:DisplayObjectContainer;

		public function StageManager():void{}
		
		public function init(_dispObj:DisplayObjectContainer,_resize:Boolean=false):void
		{
			scope = _dispObj;
			scope.stage.scaleMode 	= StageScaleMode.NO_SCALE;
			scope.stage.align		= StageAlign.TOP_LEFT;
			if(_resize)setResize();
		}
		
		/**
		 * resize 관련.
		 */
		private function setResize():void
		{
			timer = new Timer(500);
			timer.addEventListener(TimerEvent.TIMER,timerHandler);	
			scope.stage.addEventListener(Event.RESIZE,resizeHandler);
		}
		
		public function removeResize():void
		{
			timer.stop();
			timer.removeEventListener(TimerEvent.TIMER,timerHandler);	
		}
		
		// 리사이즈 시 호출 함수
		private function resizeHandler(_evnt:Event):void
		{
			timer.reset();
			timer.start();
		}
		
		// 인터벌 후 최후에 실행 함수
		private function timerHandler(_e:TimerEvent):void
		{			
			dispatchEvent(new CustomEvent(STAGE_RESIZE, {size:getStageSize()}));
		}
		
		// 현재 창 크기 구함.
		public function getStageSize():Object
		{
			return {width:scope.stage.stageWidth, height:scope.stage.stageHeight};
		}
		
		/**
		 * 풀스크린 관련.
		 */
		public function goFullScreen():void
		{
			scope.stage.displayState = StageDisplayState.FULL_SCREEN;
		}
		
		public function exitFullScreen():void
		{
			scope.stage.displayState = StageDisplayState.NORMAL;
		}
	}
}